package com.wlc.thread.demo03;

import com.wlc.thread.Hero;
import com.wlc.thread.KillThread;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 王立朝 wanglichao@champion-credit.com
 * @description join 方法
 * @date 2021-07-15 09:37
 */
public class TestThread02 {

    /**
     * 获取英雄
     *
     * @return
     */
    public List<Hero> getHero() {
        ArrayList<Hero> heroes = new ArrayList<>();
        heroes.add(new Hero("gailun", 400, 100));
        heroes.add(new Hero("timo", 300, 30));
        heroes.add(new Hero("cyj", 400, 100));
        heroes.add(new Hero("sjlr", 300, 30));
        return heroes;
    }


    /**
     * 使用join方法加入到当前线程中，只有当前线程执行完毕，后续的线程才会执行
     */
    public void threadJoin() {
        List<Hero> hero = getHero();
        Hero gailun = hero.get(0);
        Hero timo = hero.get(1);
        Hero cyj = hero.get(2);
        Hero sjlr = hero.get(3);
        System.out.println("这是第一线程下执行的--");
        KillThread killThread = new KillThread(gailun, timo);
        killThread.start();
        // join 方法 是加入到当前线程中，只有当 加入的这个线程执行完了，才可以执行后续的线程
        try {
            killThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("这是第二个线程下执行的---");
        KillThread killThread2 = new KillThread(cyj, sjlr);
        killThread2.start();

    }

    /**
     * 为线程设置优先级
     */
    public void threadPriority() {
        List<Hero> hero = getHero();
        Hero gailun = hero.get(0);
        Hero timo = hero.get(1);
        Hero cyj = hero.get(2);
        Hero sjlr = hero.get(3);
        System.out.println("这是第一线程下执行的--");
        KillThread killThread = new KillThread(gailun, timo);

        System.out.println("这是第二个线程下执行的---");
        KillThread killThread2 = new KillThread(cyj, sjlr);

        // 为线程设置优先级
        killThread.setPriority(Thread.MAX_PRIORITY);
        killThread.start();
        // Thread.MIN_PRIORITY 最小优先级
        killThread2.setPriority(Thread.MIN_PRIORITY);
        killThread2.start();

    }

    /**
     * 让当前线程临时暂停，给其他线程让路
     */
    public void threadYield() {
        List<Hero> hero = getHero();
        Hero gailun = hero.get(0);
        Hero timo = hero.get(1);
        Hero cyj = hero.get(2);
        Hero sjlr = hero.get(3);
        System.out.println("这是第一线程下执行的--");
        Thread t1 = new Thread() {
            @Override
            public void run() {
                while (!timo.isDead()) {
                    gailun.attackHero(timo);
                }
            }
        };


        System.out.println("这是第二个线程下执行的---");
        Thread t2 = new Thread() {
            @Override
            public void run() {
                while (!sjlr.isDead()) {
                    Thread.yield();
                    cyj.attackHero(sjlr);
                }
            }
        };

        // 为线程设置优先级
        t1.setPriority(Thread.NORM_PRIORITY);
        t1.start();
        // Thread.MIN_PRIORITY 最小优先级
        t2.setPriority(Thread.NORM_PRIORITY);
        t2.start();

    }

    /**
     * 守护线程： 当所有的线程都是守护线程的时候，当前进程结束
     */
    public void threadDaemon() {
        Thread t1 = new Thread() {

            @Override
            public void run() {
                int s = 0;
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("已经玩了" + s++ + "秒");
                }
            }
        };
        t1.setDaemon(false);
        t1.start();

    }


    public static void main(String[] args) {
        new TestThread02().threadDaemon();
    }
}
